<!DOCTYPE html>
<html>
<head>
</head>
<body>
<input id="user" type="text" name="user" class="form-control" autofocus = 'autofocus' placeholder="输入帐号(自动注册)" />
<span>账号必须为数字</span>
<br/>
<input id ="password" type="password" name="password" class="form-control" placeholder="请输入密码" />
<span>密码可以为任意值</span>
<br/>
<button type="button" onclick="test()">登录</button>
<script language="javascript" type="text/javascript">
    const {session} = require('electron').remote;
    const cookie = { url: 'http://localhost/index.html', name: 'dummy_name'};
    select();
    function test() {
        //window.location.href='./test.html';
        let user = document.getElementById('user').value;
        let password = document.getElementById('password').value;
        for(let i = 0;i < global.qwe.length;i++){
            if(global.qwe[i].username == user){
                if(global.qwe[i].password == password){
                    cookie.name = user;
                    session.defaultSession.cookies.set(cookie, (error) => {
                        if (error) {
                            console.error(error);
                        }
                    });
                    window.location.href='./home.html';
                }
                else {
                    alert('密码错误');
                    window.location.href='./index.html';
                }
            }
            else{
                add(user, password);
                cookie.name = user;
                session.defaultSession.cookies.set(cookie, (error) => {
                    if (error) {
                        console.error(error);
                    }
                });
                window.location.href='./home.html';
            }
        }
    }
    function add(user,password) {
        let mysql  = require('mysql');

        let connection = mysql.createConnection({
            host     : 'localhost',
            user     : 'root',
            password : 'yang1sheng',
            port: '3306',
            database: 'test',
        });

        connection.connect();

        let addSql = 'INSERT INTO user(username,password) VALUES(?,?)';
        let addSqlParams = [];
        addSqlParams.push(user);
        addSqlParams.push(password);

//增
        connection.query(addSql,addSqlParams,function (err, result) {
            if(err){
                console.log('[INSERT ERROR] - ',err.message);
                return;
            }
        });

        connection.end();

    }
    function select() {
        let mysql  = require('mysql');
        let connection = mysql.createConnection({
            host     : 'localhost',
            user     : 'root',
            password : 'yang1sheng',
            port: '3306',
            database: 'test',
        });

        connection.connect();

        let  sql = 'SELECT * FROM user';
        //查
        connection.query(sql,function (err, result) {
            if(err){
                console.log('[SELECT ERROR] - ',err.message);
                return;
            }
            global.qwe = result;
        });
        connection.end();
    }
</script>
</body>
</html>